//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CVertexLayout9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2008/12/28
///	@desc	.

#include "config/config.h"

#include "graphics/dx9/CVertexLayout9.h"
#include "graphics/dx9/CShaderAdapter9.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CVertexLayout9 );
	
	// CVertexLayout9
	CVertexLayout9::CVertexLayout9( RefWeak<IShaderAdapter> defaultAdapter ,
		IDirect3DVertexDeclaration9 * decl , const std::vector< Ref<CSlotLayout> >& slots )
		: m_defaultAdapter( defaultAdapter )
		, m_decl( decl )
		, m_slots( slots )
	{
	}
	// ~CVertexLayout9
	CVertexLayout9::~CVertexLayout9( void )
	{
		if( m_decl )
		{
			UINT rest = m_decl->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CVertexLayout9::DTOR: Invalid IDirect3DTexture9 reference count.") );
				FATAL_ABORT( _S("Invalid IDirect3DVertexDeclaration9 reference count.") );
			}
			m_decl = NULL;
		}
	}
	// ApplyLayout
	void CVertexLayout9::ApplyLayout( RenderContext& rc )
	{
		HRESULT hr = rc.GetDX9()->SetVertexDeclaration( m_decl );
		if( FAILED(hr) )
		{
			TRACE_ERROR( _S("CVertexLayout9::ApplyLayout: Failed to set vertex declaration: %s.") ,
				d3d_get_err(hr) );
		}
	}
	// GetSlotCount
	size_t CVertexLayout9::GetSlotCount( void )
	{
		return m_slots.size();
	}
	// GetSlot
	RefWeak<ISlotLayout> CVertexLayout9::GetSlot( size_t slot )
	{
		XS_ASSERT( slot < m_slots.size() );
		return m_slots[slot];
	}
	// GetDefaultAdapter
	RefWeak<IShaderAdapter> CVertexLayout9::GetDefaultAdapter( void )
	{
		return m_defaultAdapter;
	}
	
} // namespace xeres
